package test;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

/**
 * Created with IntelliJ IDEA
 * Descroption
 * User:dell
 * Date:2025-09-05
 * Time:19:47
 */
public class RegisterPage extends Utils {
    public static String url = "http://81.70.248.108:58081/register.html";
    public RegisterPage() {
        super(url);
    }

    /**
     * 检查注册页面元素是否加载
     */
    public void checkRegister(){
        //检查注册框
        webDriver.findElement(By.cssSelector("#username"));
        webDriver.findElement(By.cssSelector("#password"));
        webDriver.findElement(By.cssSelector("#register"));
    }

    /**
     * 注册失败
     */
    public void registerFail(){
        //清空输入框
        webDriver.findElement(By.cssSelector("#username"));
        webDriver.findElement(By.cssSelector("#password"));

        //1.用户密码都为空
        webDriver.findElement(By.cssSelector("#register")).click();
        //切换到弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert1 = webDriver.switchTo().alert();
        alert1.dismiss();

        //2. 用户名为空
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        webDriver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        webDriver.findElement(By.cssSelector("#register")).click();
        //处理弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert2 = webDriver.switchTo().alert();
        alert2.dismiss();

        //3.密码为空
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123");
        webDriver.findElement(By.cssSelector("#register")).click();
        //处理弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert3 = webDriver.switchTo().alert();
        alert3.dismiss();

        //.4用户名重复
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        webDriver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123");
        webDriver.findElement(By.cssSelector("#register")).click();
        //处理弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert4 = webDriver.switchTo().alert();
        alert2.dismiss();
    }

    /**
     *注册成功
     */
    public void registerSuc(String userName,String password){
        //先清空输入框
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();

        //输入注册的用户名和密码：username,password
        webDriver.findElement(By.cssSelector("#username")).sendKeys(userName);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.findElement(By.cssSelector("#register")).click();

        //处理弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = webDriver.switchTo().alert();
        //切换到弹窗
        alert.dismiss();
    }
}
